const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//获取px与rpx之间的比列 
function getRpx(px){ 
  var winWidth = wx.getSystemInfoSync().windowWidth;
  return 750/winWidth*px;
}
function getPx(rpx){
  var winWidth = wx.getSystemInfoSync().windowWidth;
  return rpx*winWidth/750;
}

const delHtmlTag =str => {
  return str.replace(/<[^>]+>/g,"");//去掉所有的html标记
} 

function isNumber(value) {
  var myreg=/[0-9]$/;
  return myreg.test(value);
}

function isValidatePhoneNumber(value) {
  var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (!myreg.test(value)) {
      return false;
  } else {
      return true;
  }
}

/* 电子邮箱 */
function isValidateEmail(value) {
  const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
  return reg.test(value)
 }

 function stringToByte(str) {
  var bytes = new Array();
  var len, c;
  len = str.length;
  for(var i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if(c >= 0x010000 && c <= 0x10FFFF) {
      bytes.push(((c >> 18) & 0x07) | 0xF0);
      bytes.push(((c >> 12) & 0x3F) | 0x80);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if(c >= 0x000800 && c <= 0x00FFFF) {
      bytes.push(((c >> 12) & 0x0F) | 0xE0);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if(c >= 0x000080 && c <= 0x0007FF) {
      bytes.push(((c >> 6) & 0x1F) | 0xC0);
      bytes.push((c & 0x3F) | 0x80);
    } else {
      bytes.push(c & 0xFF);
    }
  }
  return bytes;
}


 function byteToString(arr) {
  if(typeof arr === 'string') {
    return arr;
  }
  var str = '',
    _arr = arr;
  for(var i = 0; i < _arr.length; i++) {
    var one = _arr[i].toString(2),
      v = one.match(/^1+?(?=0)/);
    if(v && one.length == 8) {
      var bytesLength = v[0].length;
      var store = _arr[i].toString(2).slice(7 - bytesLength);
      for(var st = 1; st < bytesLength; st++) {
        store += _arr[st + i].toString(2).slice(2);
      }
      str += String.fromCharCode(parseInt(store, 2));
      i += bytesLength - 1;
    } else {
      str += String.fromCharCode(_arr[i]);
    }
  }
  return str;
}

function saveStringToFile(strData, fileName){
  var path = wx.env.USER_DATA_PATH + '/' + fileName;
  const fsm = wx.getFileSystemManager();
  fsm.writeFile({
    filePath: path,
    data: strData,
    success: function(e){
      console.log('appendFile success');
    },
    fail: function(e){
      console.log('appendFile fail');
    }
  });
}

function getStringFromFile(fileName) {
  var path = wx.env.USER_DATA_PATH + '/' + fileName;
  const fsm = wx.getFileSystemManager();
  var result;
  try {
    result = fsm.readFileSync(
      path,
      'utf-8',
    );
  } catch (e) {
    console.log(e);
  }
  return result;
}

String.prototype.replaceAll  = function(searchValue,replaceValue){     
  return this.replace(new RegExp(searchValue,"gm"),replaceValue);     
} 

module.exports = {
  formatTime: formatTime,
  getRpx: getRpx,
  getPx: getPx,
  delHtmlTag: delHtmlTag,
  isValidatePhoneNumber: isValidatePhoneNumber,
  isValidateEmail: isValidateEmail,
  isNumber: isNumber,
  stringToByte: stringToByte,
  byteToString: byteToString,
  saveStringToFile: saveStringToFile,
  getStringFromFile: getStringFromFile,
}
